package Demo19;

import java.security.SecureRandom;

//public class MyThread extends  Thread {
public class MyThread implements   Runnable {
//    extends  Thread 继承线程类
    private  int count=10;
    private  String  name;
    public  MyThread(String name){
        this.name=name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
//    super.run();添加run方法
    public void run() {
//        super.run();
        //只能取一次
//        if(count>0){
        //所有的钱可以取完
        while (count>0){
            //同步锁
            synchronized (this){
                try {
                    //休眠2秒钟----//阻塞状态
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                if(count==5){
//                    MyThread1 m=new MyThread1();
//                    m.start();
//                    try {
//                        m.join();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }

                //可以锁一个语句块/一个方法
                System.out.println(name+"取了1块钱，还有"+(--count)+"元。\n");

            }

        }
    }
}
